package com.changpay.zywy.ui.activity.web

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Handler
import android.os.Message
import com.changpay.zywy.constant.Constant

import java.lang.ref.WeakReference
import com.changpay.zywy.constant.Constant.BACK
import com.changpay.zywy.constant.Constant.CAMERA
import com.changpay.zywy.constant.Constant.LOGIN
import com.changpay.zywy.constant.Constant.PAY
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.help.ImageHelper
import com.changpay.zywy.help.PermissionHelper
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.pay.PayHelper
import com.changpay.zywy.pay.PayListener
import com.changpay.zywy.ui.activity.login.LoginActivity
import com.changpay.zywy.utils.Preference

class JsInterfaceHandler(private val context: WebViewActivity, private val payListener: PayListener) : Handler() {
    private val weakRefActivity: WeakReference<Activity> = WeakReference(context)
    /**
     * token
     */
    var token: String by Preference(Constant.TOKEN_KEY, "")

    override fun handleMessage(msg: Message) {
        val activity = weakRefActivity.get()
        if (activity != null) {
            when (msg.what) {
                BACK -> activity.finish()
                PAY -> {
                    val payInfo = msg.obj as String
                    val split = payInfo.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                    val payType = split[0]  //
                    val paySn = split[1]
                    if (payType == "alipay") {
                        aLiPay(paySn)
                    } else {
                        context.actionWechatPay()  //标识微信支付
                        wechatPay(paySn)
                    }
                }
                LOGIN-> {
                    val intent = Intent(context, LoginActivity::class.java)
                    activity.startActivity(intent)
                }
                CAMERA->
                {
                    PermissionHelper.instance.getPermission(activity, object : PermissionHelper.PermissionCallBack {
                        override fun onSuccess() {
                            ImageHelper.showImageChoose(activity)
                        }

                        override fun onFail() {
                            activity.showToast("请先允许权限再使用此功能")
                        }
                    }, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                }
            }
        }
    }

    private fun wechatPay(paysn: String) {

        val param = hashMapOf<String, String>()
        param["key"] = token
        param["pay_sn"] = paysn
        param["payment_code"] = "wxpay_app"
        payListener.payStart("wechat", paysn)
        RetrofitHelper.service.pay_new(param).sss(context)
        {
            PayHelper.INSTANCE.weChatPay(context, it.result, payListener)
        }
    }

    private fun aLiPay(paysn: String) {

        val param = hashMapOf<String, String>()
        param["key"] = token
        param["pay_sn"] = paysn
        param["payment_code"] = "alipay_app"
        payListener.payStart("alipay", paysn)
        RetrofitHelper.service.ali_pay(param).sss(context)
        {
                PayHelper.INSTANCE.aliPay(context,
                    it.result.content, payListener)
            }
        }
}